#include "threadpooltest.h"

#include <QtTest/qtest.h>

// 测试函数
void add(int a, int b)
{
	qDebug() << "a + b = " << a + b;
}
// 测试类
class Calculator
{
public:
	int product(int a, int b)
	{
		qDebug() << "class function..";
		return a * b;
	}
};

ThreadPoolTest::ThreadPoolTest(QObject* parent) : QObject(parent)
{
}

ThreadPoolTest::~ThreadPoolTest()
{
}

void ThreadPoolTest::initTestCase()
{
	qDebug() << "CLASS:ThreadPool START";
}

void ThreadPoolTest::tstThreadPool()
{
	auto instance = ThreadPool::instance();
	if (instance != nullptr)
	{
		QObject::connect(instance, &ThreadPool::finished, [&](int id, const QVariant& result) {
			if (result.isNull())
			{
				qDebug() << QString("[r%1]Void finished.").arg(id);
				return;
			}
			//.
			bool ok = false;
			// int type
			{
				int num = result.toInt(&ok);
				if (ok)
					qDebug() << QString("[r%1]Number --> %2").arg(id).arg(num);
				else
					qWarning() << QString("[r%1]Conversion failed!").arg(id);
			}
		});

		instance->addTask([]() -> QVariant { return 2 * 3; }, 111, task_priority::TP_IDLE);
		instance->addTask([] { add(1, 2); }, 333, task_priority::TP_HIGH);

		auto c = QSharedPointer<Calculator>::create();
		instance->addTask([c]() -> QVariant { return c->product(4, 5); },
						  222,
						  task_priority::TP_Emergency);

		instance->runTask();
	}
}

void ThreadPoolTest::cleanupTestCase()
{
	qDebug() << "CLASS:ThreadPool END";
}
QTEST_MAIN(ThreadPoolTest);